/*
 * ExternalSort.h
 *
 *  Created on: Oct 10, 2012
 *      Author: bruno
 */

#ifndef EXTERNALSORT_H_
#define EXTERNALSORT_H_

#include <memory>
#include <string>
#include <cstring>
#include "tools/Heap.h"
#include "FileManager.h"
#include "FileMerger.h"

typedef std::shared_ptr<DictionaryTerm> DICTIONARY_ENTRY;

int dictionaryEntryComparator(DICTIONARY_ENTRY entry1, DICTIONARY_ENTRY entry2);

class ExternalSort {
private:
	IDS_ptr input;
	IDS_ptr output;
	std::unique_ptr< Heap<DICTIONARY_ENTRY, dictionaryEntryComparator> > heap;
	std::unique_ptr<FileManager> fileManager;
	std::unique_ptr<FileMerger> fileMerger;
	unsigned int bufferSizeFactor;

	void loadHeap();
	void mergeFiles();

public:
	ExternalSort(unsigned int bufferSizeFactor, std::string workFile, std::string outputFileName, bool outputInteger);
	virtual ~ExternalSort();
	void sort();
};

#endif /* EXTERNALSORT_H_ */
